unit VIS_ENGINE;
interface
uses Classes;
procedure VIS_ENGINE_REGISTER(const Width, Height: longint; const WAVE, FFT : POINTER);
procedure VIS_ENGINE_DRAW(const p_handle: Thandle);
procedure VIS_ENGINE_TERMINATE;
implementation
uses
{$IFDEF MSWINDOWS}
    Windows,
{$ELSE}
    LCLIntf, LCLType, LMessages,
{$ENDIF}
 sdk, Graphics, spec_types, Math, SysUtils;

var
    loaded: boolean;
    a_Handle: Cardinal;
    a_WAVE: PWAVEDATA;
    a_FFT: PFFTDATA;
    m_WAVE: TWAVEDATA;
    m_FFT: TFFTDATA;
    VisBuff: TbitMap;
    spec_Height: Longint;
    penColor, PeakColor, BkgColor: Tcolor;
    {---------FOR SPEKTRUMS ONLY-----------}
    FFT_PEAKS: TFFT_PEAKS;
    FFT_FALLOFF: TFFT_FALLOFF;
procedure VIS_ENGINE_REGISTER(const Width, Height: longint; const WAVE, FFT : POINTER);
begin
    loaded := true;
   // a_Handle := Handle;
    a_WAVE := WAVE;
    a_FFT := FFT;
    if (VisBuff <> nil) then VisBuff.Free;
    VisBuff := TBitmap.Create;
    VisBuff.Width := Width;
    VisBuff.Height := Height;
    penColor := _VIS_PEN_COLOR;
    PeakColor := _VIS_PEAK_COLOR;
    BkgCOlor := _VIS_BKG_COLOR;
    Spec_Height := VisBuff.Height - 10;
    
end;

procedure VIS_ENGINE_DRAW(const p_handle: Thandle);
    procedure DRAW_FRAME(const X, Y: Integer);// Put Your code here
    var
        YVAL: single;
        YPOS: Longint;
        i: Longint;
    begin
        VisBuff.Canvas.Pen.Color := BkgColor;
        VisBuff.Canvas.Brush.Color := BkgColor;
        VisBuff.Canvas.Rectangle(0, 0, VisBuff.Width, VisBuff.Height);
        m_WAVE := a_WAVE^;
        m_FFT := a_FFT^;
        for i := 0 to _SPEC_MAX_PEAKS do
        begin
            YVAL := abs(m_FFT[(i * _VIS_DRAWRES) + 5]);
            YPOS := Trunc(YVAL * 500);
            if (YPOS > SPEC_HEIGHT) then
                SPEC_HEIGHT := YPOS;
            if (YPOS >= FFT_PEAKS[i]) then
                FFT_PEAKS[i] := YPOS
            else
                FFT_PEAKS[i] := FFT_PEAKS[i] - _SPEC_PEAKFALL;

            if (YPOS >= FFT_FALLOFF[i]) then
                FFT_FALLOFF[i] := YPOS
            else
                FFT_FALLOFF[i] := FFT_FALLOFF[i] - _SPEC_LINEFALL;
            FFT_PEAKS[i] := max(FFT_PEAKS[i], 0);
            FFT_FALLOFF[i] := max(FFT_FALLOFF[i], 0);
            case _VIS_DRAWTYPE of
            0 :
                begin
                    VisBuff.Canvas.MoveTo(X + I, Y + VisBuff.Height);
                    VisBuff.Canvas.LineTo(X + I, Y + VisBuff.Height - FFT_FALLOFF[i]);
                    if (_SPEC_SHOW_PEAKS) then
                        VisBuff.Canvas.Pixels[X + I, Y + VisBuff.Height - FFT_PEAKS[i]] := _VIS_PEN_COLOR;
                end;
            1 :
                begin
                    if (_SPEC_SHOW_PEAKS) then
                    begin
                        VisBuff.Canvas.Pen.Color := peakColor;
                        VisBuff.Canvas.MoveTo(X + I * (_SPEC_COLWIDTH + 1), Y + VisBuff.Height - FFT_PEAKS[i]);
                        VisBuff.Canvas.LineTo(X + (I + 1) * (_SPEC_COLWIDTH + 1), Y + VisBuff.Height - FFT_PEAKS[i]);
                    end;
                    VisBuff.Canvas.Pen.color := PenColor;
                    VisBuff.Canvas.Brush.Color := PenColor;
                    VisBuff.Canvas.Rectangle(X + i * (_SPEC_COLWIDTH + 1), Y +VisBuff.Height - FFT_FALLOFF[i],
                     X + (i + 1) * (_SPEC_COLWIDTH + 1), Y + VisBuff.Height);
                end;
            end;
        end;
    end;
var
  i, j: Longint;
begin
    DRAW_FRAME(0, 0);
    BitBlt(p_handle, 0, 0, VisBuff.Width, VisBuff.Height, VisBuff.Canvas.Handle, 0, 0, srccopy);
    //VisBuff.SaveToFile('d:\file' + inttostr(VisBuff.Width)+'x'+ inttostr(VisBuff.Height) + '.bmp');
end;

procedure VIS_ENGINE_TERMINATE;
begin
    loaded := false;
    if (VisBuff <> nil) then
    begin
      VisBuff.Destroy;
      VisBuff := nil;
    end;
end;

end.
